package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Comment;
import com.example.springboot.entity.Commoditys;
import com.example.springboot.entity.Users;
import com.example.springboot.mapper.CommentMapper;
import com.example.springboot.mapper.CommoditysMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

// 管理员
@RestController
@RequestMapping("/comment")
public class CommentController {
    /**
     * @RequestBody将传来的对象转化为JSON
     */
    @Resource
    private CommentMapper commentMapper;

    /**
     * 通过id查询
     */
    @GetMapping("/{id}")
    public Result<?> select(@PathVariable Long id){
        Comment comment = commentMapper.selectById(id);
        return Result.success(comment);
    }


    /**
     * 新增
     */
    @PostMapping
    public Result<?> save(@RequestBody Comment comment) {
        commentMapper.insert(comment);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping
    public Result<?> update(@RequestBody Comment comment) {
        commentMapper.updateById(comment);
        return Result.success();
    }

    /**
     * 删除
     * "/{id}"这种占位符数据获取需要@PathVariable注解来实现
     * "/{id}/{aa}"  @PathVariable Long id ,@PathVariable String aa 就可以取到两个值
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        commentMapper.deleteById(id);
        return Result.success();
    }


    /**
     * 分页模糊查询
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
//                              @RequestParam(defaultValue = "" ) Integer counts,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Comment> wrapper = Wrappers.<Comment>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Comment::getId, search);
        }
        Page<Comment> CommentPage = commentMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(CommentPage);
    }
}
